package com.gzc.just.play.last.war.sceneserver.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 队伍成员实体类
 * 
 * 对应数据库表：team_members
 * 作为Phase 4.3数据库持久化集成的一部分
 */
@Entity
@Table(name = "team_members", 
       indexes = {
           @Index(name = "idx_member_team_id", columnList = "team_id"),
           @Index(name = "idx_member_player_id", columnList = "player_id")
       },
       uniqueConstraints = {
           @UniqueConstraint(name = "uk_team_member", columnNames = {"team_id", "player_id"})
       })
@EntityListeners(AuditingEntityListener.class)
public class TeamMember {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "team_id", nullable = false, foreignKey = @ForeignKey(name = "fk_member_team"))
    @JsonIgnore
    private Team team;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player_id", nullable = false, foreignKey = @ForeignKey(name = "fk_member_player"))
    @JsonIgnore
    private Player player;
    
    @Column(name = "role", nullable = false)
    private Integer role = 0; // 0:普通成员, 1:队长
    
    @Column(name = "join_time", nullable = false)
    private LocalDateTime joinTime;
    
    @Column(name = "contribution", nullable = false)
    private Integer contribution = 0;
    
    @Column(name = "last_active_time", nullable = false)
    private LocalDateTime lastActiveTime;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // 角色常量
    public static final int ROLE_MEMBER = 0;   // 普通成员
    public static final int ROLE_LEADER = 1;   // 队长
    
    // 构造函数
    public TeamMember() {}
    
    public TeamMember(Team team, Player player, Integer role) {
        this.team = team;
        this.player = player;
        this.role = role;
        this.joinTime = LocalDateTime.now();
        this.lastActiveTime = LocalDateTime.now();
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Team getTeam() {
        return team;
    }
    
    public void setTeam(Team team) {
        this.team = team;
    }
    
    public Player getPlayer() {
        return player;
    }
    
    public void setPlayer(Player player) {
        this.player = player;
    }
    
    public Integer getRole() {
        return role;
    }
    
    public void setRole(Integer role) {
        this.role = role;
    }
    
    public LocalDateTime getJoinTime() {
        return joinTime;
    }
    
    public void setJoinTime(LocalDateTime joinTime) {
        this.joinTime = joinTime;
    }
    
    public Integer getContribution() {
        return contribution;
    }
    
    public void setContribution(Integer contribution) {
        this.contribution = contribution;
    }
    
    public LocalDateTime getLastActiveTime() {
        return lastActiveTime;
    }
    
    public void setLastActiveTime(LocalDateTime lastActiveTime) {
        this.lastActiveTime = lastActiveTime;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    // 业务方法
    public void addContribution(int amount) {
        this.contribution += amount;
        updateLastActiveTime();
    }
    
    public void updateLastActiveTime() {
        this.lastActiveTime = LocalDateTime.now();
    }
    
    public void promoteToLeader() {
        this.role = ROLE_LEADER;
    }
    
    public void demoteToMember() {
        this.role = ROLE_MEMBER;
    }
    
    public boolean isLeader() {
        return this.role == ROLE_LEADER;
    }
    
    public String getRoleName() {
        return role == ROLE_LEADER ? "队长" : "成员";
    }
    
    @Override
    public String toString() {
        return "TeamMember{" +
                "id=" + id +
                ", team=" + (team != null ? team.getTeamId() : null) +
                ", player=" + (player != null ? player.getPlayerId() : null) +
                ", role=" + role +
                ", roleName='" + getRoleName() + '\'' +
                ", contribution=" + contribution +
                ", joinTime=" + joinTime +
                ", lastActiveTime=" + lastActiveTime +
                '}';
    }
}